import { Controller, Get, Post, Body, Query, UploadedFile, UseInterceptors, HttpCode, NotFoundException, Param, Res, StreamableFile } from '@nestjs/common';
import { DownloadService } from './download.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { ApiTags, ApiOperation, ApiBody, ApiQuery } from '@nestjs/swagger';
import { ChunkFileDto, ChunkMergeFileDto, FileUploadDto, uploadIdDto } from './dto/index';
import { ResultData } from 'src/common/utils/result';
import { User, UserDto } from '../system/user/user.decorator';
import { PrismaService } from '../common/prisma/prisma.service';
import { Response } from 'express';
import Mime from 'mime-types';
import fs from 'fs';

@ApiTags('通用-文件下载')
@Controller('common/download')
export class DownloadController {
    constructor(
        private readonly downloadService: DownloadService,
        private readonly prisma: PrismaService
    ) { }

    @Post(':id')
    async downloadFile(
        @Param('id') id: string,
    ): Promise<StreamableFile> {
        try {
            const { stream, fileInfo } = await this.downloadService.getFileStream(id);
            return stream;
        } catch (error) {
            if (error instanceof NotFoundException) {
                throw new NotFoundException('File not found');
            }
            throw error;
        }
    }

    @Get(':id/info')
    async getFileInfo(@Param('id') id: string) {
        return this.downloadService.getFileInfo(id);
    }

}
